const { validate } = require("validate.js");
const { findDemoDao, addDemoDao, updateDemoDao, deleteDemoDao, findByIdDemoDao } = require("../dao/demoDao");
const { ValidationError } = require("../utils/errors");
const { formatResponse, handleDataPattern } = require("../utils/tool");

// 新增项目
module.exports.addDemoService = async function (demoInfo) {
  // 对description 项目描述转换为字符
  demoInfo.description = JSON.stringify(demoInfo.description);

  // 定义验证规则
  const demoRules = {
    name: {
      presence: {
        allowEmpty: false
      },
      type: "string"
    },
    url: {
      presence: {
        allowEmpty: false
      },
      type: "string"
    },
    description: {
      presence: {
        allowEmpty: false
      },
      type: "string"
    },
    order: {
      presence: {
        allowEmpty: false
      },
      type: "integer"
    },
    thumb: {
      type: "string"
    },
    createDate: {
      presence: {
        allowEmpty: false
      },
      type: "string"
    },
  }
  // 进行数据验证
  const validateResult = validate.validate(demoInfo, demoRules);
  if(!validateResult) {
    const data = await addDemoDao(demoInfo);
    return formatResponse(0, "", [data]);
  } else {
    throw new ValidationError("数据验证失败")
  }
}
// 查询所有项目
module.exports.findAllDemoService = async function () {
  const data = await findDemoDao();
  const obj = handleDataPattern(data);
  // 将项目描述还原成数组
  obj.forEach(item => {
    item.description = JSON.parse(item.description);
  });
  return formatResponse(0, "", obj)
}

// 查询指定项目
module.exports.findByIdDemoService = async function (id) {
  const {dataValues} = await findByIdDemoDao(id);
  dataValues.description = JSON.parse(dataValues.description);
  return formatResponse(0, "", dataValues);
}

// 修改项目
module.exports.updateDemoService = async function(id, newDemoInfo) {
  if(newDemoInfo.description) {
    newDemoInfo.description = JSON.stringify(newDemoInfo.description);
  }
  const {dataValues} = await updateDemoDao(id, newDemoInfo);
  // 还原项目描述
  dataValues.description = JSON.parse(dataValues.description);
  return formatResponse(0, "", dataValues);
}

// 删除项目
module.exports.deleteDemoService = async function (id) {
  await deleteDemoDao(id);
  return formatResponse(0, "", true);
} 